<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="welcome" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Nacos分组隔离 |  ChenyyのBlog
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">
  
<link rel="stylesheet" href="/css/custom.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-framework/Nacos分组隔离"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Nacos分组隔离
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2021/06/26/framework/Nacos%E5%88%86%E7%BB%84%E9%9A%94%E7%A6%BB/" class="article-date">
  <time datetime="2021-06-26T13:21:21.000Z" itemprop="datePublished">2021-06-26</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Spring-Cloud/">Spring Cloud</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.8k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">6 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <ul>
<li>前言</li>
<li>分组</li>
<li>隔离</li>
</ul>
<span id="more"></span>



<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>在实际开发中，通常一个系统会准备开发环境、测试环境、预发环境、正式环境。那么如何保证指定环境启动时服务能正确读取到Nacos上相应环境的配置文件呢？</p>
<p>还有就是一个系统会有很多微服务项目，每个微服务项目都会有相应的开发环境、测试环境、预发环境、正式环境，那怎么对这些微服务配置进行管理呢？</p>
<h2 id="Eureka支持分组隔离吗"><a href="#Eureka支持分组隔离吗" class="headerlink" title="Eureka支持分组隔离吗"></a>Eureka支持分组隔离吗</h2><p>eureka 作为注册中心，feign 服务之间调用，原生不支持服务的隔离。</p>
<p>要实现这个功能需要自定义负载均衡策略。在服务注册的时候，将每个服务实例分组标示好，然后自定义实现负载均衡的策略，根据服务消费者的分组名找到对应分组的服务提供者，选择性的请求就可以。</p>
<h1 id="分组"><a href="#分组" class="headerlink" title="分组"></a>分组</h1><h2 id="DataId方案"><a href="#DataId方案" class="headerlink" title="DataId方案"></a>DataId方案</h2><p>Data ID的命名规则为</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$&#123;prefix&#125;-$&#123;spring.profile.active&#125;.$&#123;file-extension&#125;</span><br></pre></td></tr></table></figure>

<p>通过其中的spring.profile.active属性即可进行多环境下配置文件的读取</p>
<h3 id="新建配置"><a href="#新建配置" class="headerlink" title="新建配置"></a>新建配置</h3><p>在Nacos控制台新建DataID为goods-consumer-dev.yml和goods-consumer-test.yml，配置项goods.crisis_stock。</p>
<p>我们dev环境设置goods.crisis_stock=10，test环境设置goods.crisis_stock=20</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626192939.png"></p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193136.png"></p>
<h3 id="多环境测试"><a href="#多环境测试" class="headerlink" title="多环境测试"></a>多环境测试</h3><p>启动goods-consumer应用，设置启动参数spring.profiles.active，设置不同的环境dev、test；访问URL请求，得到不同的值，表示多环境生效</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193307.png"></p>
<h2 id="Group方案"><a href="#Group方案" class="headerlink" title="Group方案"></a>Group方案</h2><p>也可以不用DataID，通过Group实现环境区分</p>
<h3 id="新建配置-1"><a href="#新建配置-1" class="headerlink" title="新建配置"></a>新建配置</h3><p>在nacos控制台上面新建配置文件Data ID为：goods-consumer.yml, Group为：DEV_GROUP。</p>
<p>创建配置文件Data ID为：goods-consumer.yml, Group为：TEST_GROUP。</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193500.png"></p>
<p>这里的两个配置文件他们的DataID相同但是Group不同</p>
<p>修改项目中的配置文件bootstrap.yml</p>
<p>在config下增加一条group的配置，指定配置文件所在的group，可配置为DEV_GROUP或TEST_GROUP</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193604.png"></p>
<h3 id="启动测试"><a href="#启动测试" class="headerlink" title="启动测试"></a>启动测试</h3><p>启动参数将group配置为DEV_GROUP或TEST_GROUP启动进行测试，请求URL，得到不同Group配置的值</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193718.png"></p>
<h2 id="Namespace方案"><a href="#Namespace方案" class="headerlink" title="Namespace方案"></a>Namespace方案</h2><p>Namespace命名空间进行环境隔离也是官方推荐的一种方式。Namespace的常用场景之一是不同环境的配置的区分隔离，例如：开发测试环境和生产环境的资源(如配置、服务)隔离等。</p>
<p>创建命名空间</p>
<p>创建命名空间DEV、TEST、PROD,不同的命名空间会生成相应的UUID，如下图</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193819.png"></p>
<h3 id="新建配置文件"><a href="#新建配置文件" class="headerlink" title="新建配置文件"></a>新建配置文件</h3><p>在命名空间DEV下创建DataID为：goods-consumer-dev.yml，Group为默认值的配置,配置如下：</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193836.png"></p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193846.png"></p>
<p>在命名空间TEST下创建DataID为：goods-consumer-test.yml,Group为默认值的配置。</p>
<p>新建项目中的配置文件bootstrap-dev.yml，bootstrap-test.yml</p>
<p>在config下增加一条namespace的配置，指定当前配置所在的命名空间ID。注意是命名空间ID</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193926.png"></p>
<h3 id="启动测试-1"><a href="#启动测试-1" class="headerlink" title="启动测试"></a>启动测试</h3><p>为什么要新建bootstrap-dev.yml和bootstrap-test.yml；就是为了方便启动参数进行设置，区分dev环境和test环境不同的配置；启动参数-Dspring.profiles.active</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193940.png"></p>
<h1 id="隔离"><a href="#隔离" class="headerlink" title="隔离"></a>隔离</h1><p>以上介绍了DataID，Group，Namespace三种方式可以实现多环境；一般在公司我们会多个项目共用同一个Nacos集群，那多个项目的多个环境如何设计呢？下面我们就讲解单租户和多租户的设计方案。</p>
<h2 id="单租户实践方案一"><a href="#单租户实践方案一" class="headerlink" title="单租户实践方案一"></a>单租户实践方案一</h2><p>如果有多套不同的环境，那么这个时候可以根据指定的环境来创建不同的namespce，以此来实现多环境的隔离。</p>
<p>例如，你可能有dev，test和prod三个不同的环境，那么使用nacos可以分别建以下三个不同的 namespace。如下图所示：</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626193952.png"></p>
<p>这种方案适于小型项目，或者是项目不太多时的实施方案。</p>
<p>通过定义不同的环境，不同环境的项目在不同的Namespace下进行管理，不同环境之间通过Namespace进行隔离</p>
<p>当多个项目同时使用该Nacos时，还可以通过Group进行Namespace内的细化分组；这里以Namespace：dev为例，在Namespace中通过不同Group进行同一环境中不同项目的再分类</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626194034.png"></p>
<p>通过以上实验，方案1可以达到多环境多项目下的服务、配置管理的目标</p>
<p>方案1通过Namespace来隔离不同的环境(dev\test),在具体的环境Namespace中通过Group来管理不同的项目</p>
<h2 id="多租户实施方案二"><a href="#多租户实施方案二" class="headerlink" title="多租户实施方案二"></a>多租户实施方案二</h2><p>在一些中大型公司，一般会多个人各自负责多个项目，那么每个人可能会有自己的 namespace,每个人的配置数据以及注册的服务数据都会归属到自己的namespace下，以此来实现多租户间的数据隔离。</p>
<p>例如超级管理员分配了三个租户，分别为张三、李四和王五。张三负责A项目，李四负责B项目，王五负责C项目</p>
<p>分配好了之后，各租户用自己的账户名和密码登录后，创建自己的命名空间。</p>
<p>注：现在nacos权限管理还是不是太完善，期待下个版本</p>
<p>如下图所示：</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626194056.png"></p>
<p>方案2通过Namespace来隔离多租户之间的服务和配置，但不仅于此，他有很好的扩展性</p>
<p>公司发展迅速业务调整，张三负责A项目、B项目、C项目，李四负责D项目、E项目、F项目，王五负责G项目、H项目、I项目。</p>
<p>而每个项目又分了dev、test、prod三个环境，继续沿用之前的Namespace隔离租户方案，显得有些管理不便，这时候可以在NameSpace中加入Group进行项目环境分组，有两种方式。如图：</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626194110.png"></p>
<p>另一个方式，张三、李四、王五每人都负责10多个项目时，即项目数&gt;环境数时，可以通过Group进行项目分组，如下图：</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626194110.png"></p>
<p>通过上面的理论分析，可以看出方案二有很好的扩展性</p>
<p>依旧如上，我们通过代码来实践一下方案2(Namespace隔离租户 + group环境分组)</p>
<p>关于单租户和多租户的演示，小伙伴们自行实践一下，只要明白设计思路，操作是很简单的</p>
<p>总结</p>
<p>以上分析了Nacos对于Namespace提供的两种实践方案，</p>
<p>1、单租户方案(方案一)：适合小型项目，服务数量不多时，方案一完全够用</p>
<p>2、多租户方案(方案二)：适合项目量多，有一定的团队规模，且服务数量较多时，可以相对条理清晰的管理和隔离配置及服务。</p>
<p>参考：</p>
<ul>
<li>eureka多环境隔离_Nacos高级应用之多环境多项目管理：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_36091906/article/details/112105951%EF%BC%8Chttps://blog.csdn.net/weixin_34640046/article/details/112105953">https://blog.csdn.net/weixin_36091906/article/details/112105951，https://blog.csdn.net/weixin_34640046/article/details/112105953</a></li>
<li>多环境下如何“读取”Nacos中相应的配置：<a target="_blank" rel="noopener" href="https://www.larscheng.com/config-profile/">https://www.larscheng.com/config-profile/</a></li>
<li>多环境下如何“管理”及“隔离”配置和服务：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/larscheng/p/11411423.html">https://www.cnblogs.com/larscheng/p/11411423.html</a></li>
<li>spring cloud eureka 服务分组隔离：<a target="_blank" rel="noopener" href="https://blog.csdn.net/lujia_loney/article/details/113866613">https://blog.csdn.net/lujia_loney/article/details/113866613</a></li>
</ul>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2021/06/26/framework/Nacos%E5%88%86%E7%BB%84%E9%9A%94%E7%A6%BB/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Spring-Cloud/" rel="tag">Spring Cloud</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/06/26/framework/Feign%E6%9C%8D%E5%8A%A1%E8%B0%83%E7%94%A8%E5%92%8C%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Feign服务调用和负载均衡
          
        </div>
      </a>
    
    
      <a href="/2021/06/25/component/redis%E4%B8%ADhash%E6%89%A9%E5%AE%B9%E8%BF%87%E7%A8%8B/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">redis中hash扩容过程</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2021
        <i class="ri-heart-fill heart_icon"></i> Chenyy
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="ChenyyのBlog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">文章</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/book">收藏</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about/me">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>